﻿using System;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine.Sprites.Collections;
using SmlEngine.Sprites.Settings;
using SmlEngine.UI;
using SmlEngine.UI.Input;
using SmlEngine.UI.Managers;
using SmlEngine.UI.Screens;

namespace Sml.Scenes
{
    /// <summary>
    /// Encapsulates and manages a Level.
    /// </summary>
    public sealed class LevelScene : IScene
    {
        #region Properties

        #region IScene Properties

        /// <summary>
        /// The screen that owns this scene.
        /// </summary>
        public Screen Screen { get; set; }

        /// <summary>
        /// The visible area of the scene.
        /// </summary>
        public Rectangle? Area { get; set; }

        /// <summary>
        /// Stores the debugging state.
        /// </summary>
        public bool IsDebugging { get; set; }

        #endregion

        /// <summary>
        /// The Level this scene owns.
        /// </summary>
        public Level Level { get; set; }
         
        #endregion

        #region Methods

        /// <summary>
        /// Constructs a new LevelScene instance.
        /// </summary>
        /// <param name="screen">The screen that owns this scene.</param>
        public LevelScene(Screen screen)
        {
            Screen = screen;
            Level = new Level();
            Level.Input = Screen.ScreenManager.Input;
        }

        /// <summary>
        /// Initializes this LevelScene.
        /// </summary>
        public void Initialize()
        {
        }

        /// <summary>
        /// Loads and starts a level from a stream.
        /// </summary>
        /// <param name="stream">The stream to use.</param>
        public void Load(Stream stream)
        {
            Level.Reload(stream);
            Level.Start();
        }

        /// <summary>
        /// Loads the content for this level.
        /// </summary>
        /// <param name="content">The ContentManager to use.</param>
        public void LoadContent(ContentManager content)
        {
            using (Stream stream = TitleContainer.OpenStream(@"Content\tilesettings.dat"))
                TileSettings.Load(content, stream);

            using (Stream stream = TitleContainer.OpenStream(@"Content\backgroundsettings.dat"))
                BackgroundSettings.Load(content, stream);

            PlayerSettings.Load(content);
        }

        /// <summary>
        /// Reloads the content.
        /// </summary>
        public void ReloadContent() { }

        /// <summary>
        /// Updates this LevelScene instance.
        /// </summary>
        /// <param name="gameTime">The timing information of the game.</param>
        /// <param name="input">The keyboard or controller input for this frame.</param>
        public void Update(GameTime gameTime, InputManager input)
        {
            Level.Update(gameTime);
        }

        /// <summary>
        /// Draws this LevelScene to the screen.
        /// </summary>
        /// <param name="gameTime">The timing information of the game.</param>
        /// <param name="spriteBatch">The SpriteBatch to draw to.</param>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Level.Draw(gameTime, spriteBatch);
            spriteBatch.DrawString(Screen.ScreenManager.DefaultFont, Level.Players[0].CollisionComponent.IsOnGround.ToString(), Vector2.Zero, Color.Black);
        }

        /// <summary>
        /// Starts debugging.
        /// </summary>
        public void Debug()
        {
            IsDebugging = true;
        }

        #endregion
    }
}
